/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author Green
 */
public class GamblerManager {

    ArrayList<Gambler> lstGamblers;
    Gambler player;
    int screenWidth = 0;
    int screenHeight = 0;

    //Lấy thằng do client điều khiển ra
    public Gambler getMainPlayer() {
        return this.player;
    }

    public GamblerManager(int width, int height) {
        this.screenWidth = width;
        this.screenHeight = height;

        this.lstGamblers = new ArrayList<Gambler>(4);
        this.initGamblers();
        this.player = this.lstGamblers.get(0);
    }

    private void initGamblers() {
        Gambler gambler = new Gambler(0, "");
        gambler.position = new Point();
        gambler.position.x = (this.screenWidth - (GUICard.width - gambler.overlap.x) * 12 - GUICard.width) / 2;
        gambler.position.y = this.screenHeight - GUICard.height - 20;
        gambler.namePosition = new Point();
        gambler.namePosition.x = gambler.position.x;
        gambler.namePosition.y = gambler.position.y - 10;
        gambler.direction = new Point(1, 0);
        gambler.bankerDirection = new Point(0, -1);
        this.lstGamblers.add(gambler);

        gambler = new Gambler(1, "");
        gambler.position = new Point();
        gambler.position.x = 40;
        gambler.position.y = (this.screenHeight - (GUICard.height - gambler.overlap.y) * 12 - GUICard.height) / 2;
        gambler.namePosition = new Point();
        gambler.namePosition.x = gambler.position.x;
        gambler.namePosition.y = gambler.position.y - 4;
        gambler.direction = new Point(0, 1);
        gambler.bankerDirection = new Point(1, 0);
        this.lstGamblers.add(gambler);

        gambler = new Gambler(2, "");
        gambler.position = new Point();
        gambler.position.x = (this.screenWidth - (GUICard.width - gambler.overlap.x) * 12 - GUICard.width) / 2;
        gambler.position.y = 20;
        gambler.namePosition = new Point();
        gambler.namePosition.x = gambler.position.x;
        gambler.namePosition.y = gambler.position.y - 4;
        gambler.direction = new Point(1, 0);
        gambler.bankerDirection = new Point(0, 1);
        this.lstGamblers.add(gambler);

        gambler = new Gambler(3, "");
        gambler.position = new Point();
        gambler.position.x = this.screenWidth - GUICard.width - 40;
        gambler.position.y = (this.screenHeight - (GUICard.height - gambler.overlap.y) * 12 - GUICard.height) / 2;
        gambler.namePosition = new Point();
        gambler.namePosition.x = gambler.position.x;
        gambler.namePosition.y = gambler.position.y - 4;
        gambler.direction = new Point(0, 1);
        gambler.bankerDirection = new Point(-1, 0);
        this.lstGamblers.add(gambler);

    }

    public void initPlayers(String clientName, ArrayList<String> allsClientsName) {
        int playerIndex = allsClientsName.indexOf(clientName);
        assert playerIndex == -1 : "Invalid client's name";

        int i = 0;
        ArrayList<String> list = (ArrayList<String>) allsClientsName.clone();

        while (list.size() > 1) {
            Gambler gambler = this.lstGamblers.get(i);
            gambler.setName(list.get(playerIndex));
            list.remove(playerIndex);
            i++;
        }

//        i = playerIndex;
        for (int j = 0; j < list.size(); j++) {
            i = i % 4;
            Gambler gambler = this.lstGamblers.get(i);
            gambler.setName(list.get(j));
            list.remove(j);
            i++;
        }

        i = playerIndex;
    }

    public Gambler getGambler(String name) {
        for (int i = 0; i < this.lstGamblers.size(); i++) {
            Gambler gambler = this.lstGamblers.get(i);
            if (gambler.getName() == name) {
                return gambler;
            }
        }

        return null;
    }

    public Gambler getGamblerByArrayIndex(int arrayIndex) {
        if(arrayIndex < 0 || arrayIndex >= this.lstGamblers.size()) {
            return null;
        }

        return this.lstGamblers.get(arrayIndex);
    }

    public int getArrayIndexOfGambler(Gambler gambler) {
        return this.lstGamblers.indexOf(gambler);
    }

    public void passCards(int direction) {
        switch (direction) {
            case 0: {
                passLeft();
                break;
            }
            case 1: {
                passRight();
                break;
            }
            case 2: {
                passUp();
                break;
            }
        }
    }

    private void passLeft() {
        ArrayList<GUICard> list = new ArrayList<GUICard>();
        ArrayList<GUICard> anotherlist = new ArrayList<GUICard>();
        ArrayList<Gambler> glist = new ArrayList<Gambler>(this.lstGamblers);
        glist.remove(0);
        list = player.popAllsSelectedCards();

        for (int i = 0; i < 3; i++) {
            Gambler from = glist.get(i);
            anotherlist = from.popAllsSelectedCards();
            from.receivePassedCards(list);
            list = anotherlist;
            from.hideAllsAvailsCard();
        }

        player.receivePassedCards(list);
        player.showAllsAvailsCard();
    }

    private void passRight() {
        ArrayList<GUICard> list = new ArrayList<GUICard>();
        ArrayList<GUICard> anotherlist = new ArrayList<GUICard>();
        ArrayList<Gambler> glist = new ArrayList<Gambler>(this.lstGamblers);
        glist.remove(0);
        list = player.popAllsSelectedCards();

        for (int i = 3; i >= 0; i--) {
            Gambler from = glist.get(i);
            anotherlist = from.popAllsSelectedCards();
            from.receivePassedCards(list);
            list = anotherlist;
            from.hideAllsAvailsCard();
        }

        player.receivePassedCards(list);
        player.showAllsAvailsCard();
    }

    private void passUp() {
        ArrayList<Gambler> glist = new ArrayList<Gambler>(this.lstGamblers);

        for (int i = 0; i < 4; i += 2) {
            Gambler from = this.lstGamblers.get(i);
            Gambler to = this.lstGamblers.get(i);

            ArrayList<GUICard> list_from = from.popAllsSelectedCards();
            ArrayList<GUICard> list_to = to.popAllsSelectedCards();

            from.receivePassedCards(list_to);
            to.receivePassedCards(list_from);

            if(i == 0) {
                from.showAllsAvailsCard();
                to.hideAllsAvailsCard();
            } else {
                from.hideAllsAvailsCard();
                to.hideAllsAvailsCard();
            }
        }

    }

    public void turnUpPlayersCards() {
        this.player.showAllsAvailsCard();
    }

    public void drawAllsCards(Graphics g) {
        for (int i = 0; i < this.lstGamblers.size(); i++) {
            Gambler gambler = this.lstGamblers.get(i);
            gambler.drawAllsCards(g);
        }
    }

    public void drawAllsNames(Graphics g) {
        for (int i = 0; i < this.lstGamblers.size(); i++) {
            Gambler gambler = this.lstGamblers.get(i);
            gambler.drawName(g);
        }
    }

    public void drawAllsTakenCards(Graphics g) {
        for (int i = 0; i < this.lstGamblers.size(); i++) {
            Gambler gambler = this.lstGamblers.get(i);
            gambler.drawTakenCard(g);
        }
    }

    public void resetAllsGamblers() {
        for (int i = 0; i < this.lstGamblers.size(); i++) {
            Gambler gambler = this.lstGamblers.get(i);
            gambler.reset();
        }
    }
}
